package se.webbzon.boltzmann.sound;

import se.webbzon.boltzmann.debug.Debug;
import se.webbzon.boltzmann.resource.Resources;
import se.webbzon.oschi01.sound.FiniteSound;
import se.webbzon.oschi01.sound.SoundException;

public class SoundUtils {
	
	// The singleton sound instance
	private static Sounds singleton = null;
	private static FiniteSound sound = null;
	
	/*============================================================
	Public Methods
	============================================================*/
	
	/** Plays the sound with the given resource name. **/
	public static final void playSound(String soundName) {
		try {
			Resources.getSound(soundName).createSound().play();
		} catch (SoundException e) {
			Debug.report(e);
		}
	}
	
	/** Plays a singleton sound with the given resource name. If another
	 * singleton sound is currently playing then it is stopped. **/
	public static final void playSingletonSound(Sounds sound, boolean loop) {
		stopSingletonSound();
		
		if (sound != Sounds.silence) try {
			SoundUtils.sound = Resources.getSound(sound.getResourceName()).createSound();
			SoundUtils.sound.play();
			SoundUtils.sound.setLooping(loop);
			singleton = sound;
		} catch (SoundException e) {
			Debug.report(e);
		}
	}
	
	/** Stops the currently playing singleton sound. **/
	public static final void stopSingletonSound() {
		if (singleton != null && sound.isPlaying())
			sound.stop();
		singleton = null;
	}
	
	/** Returns true if the provided singleton sound is already playing. **/
	public static final boolean playingSingletonSound(Sounds sound) {
		return (singleton == sound && SoundUtils.sound.isPlaying());
	}

}
